package com.dnw.shyfunny;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;

import com.dnw.base.BaseActivity;
import com.dnw.modle.Result;
import com.dnw.presenter.ApprovedPresenter;
import com.dnw.util.AppUtil;
import com.dnw.util.SharedPreferencesMgr;
import com.dnw.view.IApprovedView;

import static com.dnw.base.ApiConstant.SharedPreferencesEnum.PASSED;
import static com.dnw.base.ApiConstant.approvedEnum.APPROVED;
import static com.dnw.base.ApiConstant.approvedEnum.NOT_APPROVED;

/**
 * @author 杨瑜
 * @description 欢迎界面
 */
public class WelcomeActivity extends BaseActivity<ApprovedPresenter> implements Callback, IApprovedView {
    private Handler handler;

    @Override
    protected ApprovedPresenter createPresenter() {
        return new ApprovedPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initView() {
        super.initView();
    }

    @Override
    public void initData() {
        super.initData();
        hasPassed();
        handler = new Handler(this);
        handler.sendEmptyMessageDelayed(1, 1500);
    }

    private void hasPassed() {
        if(!SharedPreferencesMgr.getBoolean(PASSED.name() + AppUtil.getVersionCode(this), NOT_APPROVED.isStatus())){
            mPresenter.getIsApprovedResult();
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        checkVersion();
        return false;
    }


    private void checkVersion() {
        // 版本号等于当前版本号
        String versionLast = SharedPreferencesMgr.getString("LAST_VERSION",
                "0.9.0");
        String versionCurrent = getAppVerisonCode(this);
        if (versionCurrent.equals(versionLast)) {
            startActivity(new Intent(this, MainActivity.class));
        } else {
            startActivity(new Intent(this, NewVersionGuideActivity.class));
            SharedPreferencesMgr.setString("LAST_VERSION",
                    versionCurrent);
        }
        finish();
    }

    /**
     * 获取应用版本号
     */
    public static String getAppVerisonCode(Context context) {
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }

    @Override
    public void onGetPassedSuccess(Result result) {
        double passed = (double) result.data;
        SharedPreferencesMgr.setBoolean(PASSED.name() + AppUtil.getVersionCode(this), APPROVED.getCode() == passed);
    }

    @Override
    public void onError() {

    }
}
